package com.sykj.manage.web;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.manage.service.SysLoginService;

/**
 * 
 * @author 高志远
 *
 */
@Controller
@RequestMapping("/sys")
public class SysLoginController {
	
	@Autowired
	
	SysLoginService sysLoginService;

	
		@RequestMapping("/login")
		@ResponseBody
		public String login(String account,String password){
			String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
			//SecurityUtils  是securmanager的辅助工具，获得当前用户
			Subject currentUser = SecurityUtils .getSubject();
			//如果没有认证进行登陆
			if (!currentUser.isAuthenticated()) {
				//创建账号密码对象
				UsernamePasswordToken token = new UsernamePasswordToken(account,password);
				try{
				//登陆认证操作
				currentUser.login(token);
				System.out.println("登陆成功");
				result = String.format(result, "1","登陆成功");
				return result;
				
			}catch (AuthenticationException ae){ 
				System.out.println("登陆失败");
				result = String.format(result, "0","登陆失败");
				return result;
			   }
				
			}
			return result;
		}
}
